package com.tuanzi.loan.business.repository.settlement.impl;

import static com.tuanzi.loan.generated.jooq.loan.Tables.*;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.settlement.custom.SettlementUvRecordRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.settlement.SettlementUvReportRequest;
import com.tuanzi.loan.vo.response.settlement.SettlementUvReportResponse;

/**
 * @author caiwanlin
 * @create 2018-03-30 11:16
 */
public class SettlementUvRecordRepositoryImpl extends JOOQBaseRepository implements SettlementUvRecordRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_SETTLEMENT_UV_RECORD;
    }

    /**
     * 分页查找uv报表
     * @param searchRequest
     * @param pageable
     * @return
     */
    @Override
    public Page<SettlementUvReportResponse> list(SettlementUvReportRequest searchRequest, Pageable pageable) {
        fields(LOAN_SETTLEMENT_UV_RECORD.ID,
                LOAN_SETTLEMENT_UV_RECORD.BEGIN_DATE,
                LOAN_SETTLEMENT_UV_RECORD.END_DATE,
                LOAN_SETTLEMENT_UV_RECORD.PARTICIPATE_UV,
                LOAN_SETTLEMENT_UV_RECORD.UV_VALUE,
                LOAN_PRODUCT_INFO.NAME,
                LOAN_COMPANY_INFO.COMPANY_NAME);
        leftJoin(LOAN_PRODUCT_INFO,LOAN_PRODUCT_INFO.ID.eq(LOAN_SETTLEMENT_UV_RECORD.PRODUCT_ID));
        leftJoin(LOAN_COMPANY_INFO,LOAN_COMPANY_INFO.ID.eq(LOAN_SETTLEMENT_UV_RECORD.COMPANY_ID));
        ge(LOAN_SETTLEMENT_UV_RECORD.BEGIN_DATE,searchRequest.getBeginDate());
        le(LOAN_SETTLEMENT_UV_RECORD.END_DATE,searchRequest.getEndDate());
        eq(LOAN_SETTLEMENT_UV_RECORD.PRODUCT_ID,searchRequest.getProductId());
        eq(LOAN_SETTLEMENT_UV_RECORD.COMPANY_ID,searchRequest.getCompanyId());
        eq(LOAN_SETTLEMENT_UV_RECORD.IS_GROUP,searchRequest.getIsGroup());

        return search(pageable, new RecordMapper<Record, SettlementUvReportResponse>() {
            @Override
            public SettlementUvReportResponse map(Record record) {
                SettlementUvReportResponse response = new SettlementUvReportResponse();
                response.setId(record.get(LOAN_SETTLEMENT_UV_RECORD.ID));
                response.setBeginDate(record.get(LOAN_SETTLEMENT_UV_RECORD.BEGIN_DATE));
                response.setEndDate(record.get(LOAN_SETTLEMENT_UV_RECORD.END_DATE));
                response.setCompanyName(record.get( LOAN_COMPANY_INFO.COMPANY_NAME));
                response.setProductName(record.get(LOAN_PRODUCT_INFO.NAME));
                response.setUvValue(record.get(LOAN_SETTLEMENT_UV_RECORD.UV_VALUE));
                response.setParticipateUv(record.get(LOAN_SETTLEMENT_UV_RECORD.PARTICIPATE_UV));
                return response;
            }
        });
    }
}
